package cn.tedu._05weibo.controller;

import cn.tedu._05weibo.common.response.JsonResult;
import cn.tedu._05weibo.common.response.StatusCode;
import cn.tedu._05weibo.mapper.UserMapper;
import cn.tedu._05weibo.pojo.dto.UserLoginDto;
import cn.tedu._05weibo.pojo.dto.UserRegDTO;
import cn.tedu._05weibo.pojo.entity.User;
import cn.tedu._05weibo.pojo.vo.UserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import javax.xml.ws.RespectBinding;
import java.util.Date;
@Api(tags = "用户模块")
@Slf4j
@RestController
@RequestMapping("/v1/users")
public class UserController {
    @Autowired
    private UserMapper userMapper;
    /**
     * 注册功能
     */
    @ApiOperation("注册功能")
    @PostMapping("reg")
    public JsonResult reg(@RequestBody @Validated UserRegDTO userRegDTO){
//        System.out.println("userRegDTO = " + userRegDTO);
        log.debug("userRegDTO"+userRegDTO);
        //校验用户名是否被占用
        UserVO userVO = userMapper.selectByUsername( userRegDTO.getUsername());
        if(userVO!=null){
//            JsonResult jsonResult =new JsonResult();

            return new JsonResult(StatusCode.USERNAME_ALREADY_EXISTS);
        }else {
            User user = new User();
            BeanUtils.copyProperties(userRegDTO,user);
            user.setCreated(new Date());
            userMapper.insert(user);

            return JsonResult.ok();
        }
    }



    /**
     * 登录功能
     */
    @ApiOperation("登录功能")
    @PostMapping("login")
    public JsonResult login(@RequestBody UserLoginDto userLoginDto,@ApiIgnore HttpSession session){
//        System.out.println("userLoginDto = " + userLoginDto);
        log.debug("userLoginDto"+userLoginDto);
        UserVO userVO=userMapper.selectByUsername(userLoginDto.getUsername());
        if (userVO==null){
            return new JsonResult(StatusCode.USERNAME_ERROR);//用户名错误
        }else if (userVO.getPassword().equals(userLoginDto.getPassword())){
            //设置唯一标识，用于会话保持
            session.setAttribute("user",userVO);
            return new JsonResult(StatusCode.LOGIN_SUCCESS);//登陆成功
        }else {
            return new JsonResult(StatusCode.PASSWORD_ERROR);//密码错误
        }
    }
    /**
     * 获取当前用户功能
     */
    @ApiOperation("获取当前用户功能")
    @GetMapping("currentUser")
    public JsonResult currentUser(@ApiIgnore HttpSession session){
        UserVO userVO =(UserVO) session.getAttribute("user");
        return JsonResult.ok(userVO);

    }
    /**
     * 退出登录
     */
    @ApiOperation("退出登录")
    @GetMapping("logout")
    public void logout( @ApiIgnore HttpSession session){
        //删除服务器端的会话唯一标识
        session.removeAttribute("user");
    }
}
